package com.ch.ebusiness.service.admin.impl;


import com.ch.ebusiness.entity.BUser;
import com.ch.ebusiness.repository.admin.UserAndOrderAndOutRepository;
import com.ch.ebusiness.service.admin.UserAndOrderAndOutService;
import com.ch.ebusiness.util.MD5Util;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class UserAndOrderAndOutServiceImpl implements UserAndOrderAndOutService {
    @Resource
    private UserAndOrderAndOutRepository userAndOrderAndOutRepository;
    
    @Override
    public String selectUser(Model model, int currentPage) {
        //共多少个用户
        int totalCount = userAndOrderAndOutRepository.selectAllUser();
        //计算共多少页
        int pageSize = 5;
        int totalPage = (int) Math.ceil(totalCount * 1.0 / pageSize);
        List<Map<String, Object>> typeByPage = userAndOrderAndOutRepository.selectUserByPage((currentPage - 1) * pageSize, pageSize);
        model.addAttribute("allUsers", typeByPage);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        return "admin/allUser";
    }
    
    @Override
    public String deleteUser(Model model, int id) {
        if (userAndOrderAndOutRepository.selectCartUser(id).size() > 0
                || userAndOrderAndOutRepository.selectOrderUser(id).size() > 0) {
            return "no";
        } else {
            userAndOrderAndOutRepository.deleteUser(id);
            return "/selectUserDelete?currentPage=1";
        }
    }
    
    @Override
    public String selectOrder(Model model, int currentPage) {
        //共多少个订单
        int totalCount = userAndOrderAndOutRepository.selectAllOrder();
        //计算共多少页
        int pageSize = 5;
        int totalPage = (int) Math.ceil(totalCount * 1.0 / pageSize);
        List<Map<String, Object>> orderByPage = userAndOrderAndOutRepository.selectOrderByPage((currentPage - 1) * pageSize, pageSize);
        model.addAttribute("allOrders", orderByPage);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        return "admin/allOrder";
    }
    
    @Override
    public String selectUserUpdate(Model model, int currentPage) {
        //共多少个用户
        int totalCount = userAndOrderAndOutRepository.selectAllUser();
        //计算共多少页
        int pageSize = 5;
        int totalPage = (int) Math.ceil(totalCount * 1.0 / pageSize);
        List<Map<String, Object>> typeByPage = userAndOrderAndOutRepository.selectUserByPage((currentPage - 1) * pageSize, pageSize);
        model.addAttribute("allUsers", typeByPage);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        return "admin/updateUser";
    }
    
    @Override
    public String selectUserDelete(Model model, int currentPage) {
        //共多少个用户
        int totalCount = userAndOrderAndOutRepository.selectAllUser();
        //计算共多少页
        int pageSize = 5;
        int totalPage = (int) Math.ceil(totalCount * 1.0 / pageSize);
        List<Map<String, Object>> typeByPage = userAndOrderAndOutRepository.selectUserByPage((currentPage - 1) * pageSize, pageSize);
        model.addAttribute("allUsers", typeByPage);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        return "admin/deleteUser";
    }
    
    @Override
    public String updateUser(BUser bUser) {
        //对密码MD5加密
        bUser.setBpwd(MD5Util.MD5(bUser.getBpwd()));
        userAndOrderAndOutRepository.updateUser(bUser);
        return "redirect:/selectUserUpdate?currentPage=1";
    }
    
    
    @Override
    public String deleteOrder(Model model, int id) {
        if (userAndOrderAndOutRepository.selectOrderGoods(id).size() > 0) {
            return "no";
        } else {
            userAndOrderAndOutRepository.deleteOrder(id);
            return "/selectOrderDelete?currentPage=1";
        }
    }
    
    @Override
    public String selectOrderDelete(Model model, int currentPage) {
        //共多少个订单
        int totalCount = userAndOrderAndOutRepository.selectAllOrder();
        //计算共多少页
        int pageSize = 5;
        int totalPage = (int) Math.ceil(totalCount * 1.0 / pageSize);
        List<Map<String, Object>> orderByPage = userAndOrderAndOutRepository.selectOrderByPage((currentPage - 1) * pageSize, pageSize);
        model.addAttribute("allOrders", orderByPage);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        System.out.println("jjljl");
        return "admin/deleteOrder";
        
    }
    
}
